package com.company.cloud.mall.modules.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.company.cloud.art.modules.api.response.Artist;
import org.springframework.util.CollectionUtils;

import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.company.modules.api.response.CompanySimple;
import com.company.cloud.mall.modules.api.response.OrderAfterModel;
import com.company.cloud.mall.modules.api.response.OrderItemModel;
import com.company.cloud.mall.modules.api.response.OrderLogModel;
import com.company.cloud.mall.modules.api.response.OrderModel;
import com.company.cloud.mall.modules.api.response.OrderPayModel;
import com.company.cloud.mall.modules.api.response.OrderRefundsModel;
import com.company.cloud.mall.modules.api.response.OrderReturnsModel;
import com.company.cloud.mall.modules.api.response.OrderShippingModel;
import com.company.cloud.mall.modules.api.response.OrderTicketCodeModel;
import com.company.cloud.mall.modules.base.entity.OrderInfo;
import com.company.cloud.mall.modules.base.entity.OrderItem;
import com.company.cloud.mall.modules.base.entity.OrderItemTicketCode;
import com.company.cloud.mall.modules.base.entity.OrderLog;
import com.company.cloud.mall.modules.base.entity.OrderPayment;
import com.company.cloud.mall.modules.base.entity.OrderRefunds;
import com.company.cloud.mall.modules.base.entity.OrderReturns;
import com.company.cloud.mall.modules.base.entity.OrderShipping;
import com.company.cloud.user.modules.api.response.UserSimple;

import cn.hutool.core.bean.BeanUtil;

public class ShopOrderConvertUtils {

    /**
     * 订单明细
     */
    public static OrderItemModel getOrderItemModel(OrderItem item) {
        OrderItemModel model = new OrderItemModel();
        BeanUtil.copyProperties(item, model);
        return model;
    }

    /**
     * 订单明细
     */
    public static OrderItemModel getOrderItemModel(OrderItem item, List<OrderRefunds> refundList, List<OrderReturns> returnList) {
        OrderItemModel model = new OrderItemModel();
        BeanUtil.copyProperties(item, model);

//		String ingRefundSN=null;
//		String ingReturnSN=null;
        //退款
        if (!CollectionUtils.isEmpty(refundList)) {
            for (OrderRefunds refundItem : refundList) {
                if (model.getId().intValue() == refundItem.getOrderItemId().intValue()) {
                    OrderRefundsModel orm = getOrderRefunds(refundItem);
                    model.addRefunds(orm);
//					if(OrderRefundStateEnum.ORFS_Check_Fail.getCode().equals(orm.getState())
//							|| OrderRefundStateEnum.ORFS_Complete.getCode().equals(orm.getState())
//							|| OrderRefundStateEnum.ORFS_Cancel.getCode().equals(orm.getState())) {
//					}else {
//						ingRefundSN=orm.getSn();
//					}
                }
            }
        }
        //退款退货
        if (!CollectionUtils.isEmpty(returnList)) {
            for (OrderReturns returnItem : returnList) {
                if (model.getId().intValue() == returnItem.getOrderItemId().intValue()) {
                    OrderReturnsModel orm = getOrderReturns(returnItem);
                    model.addReturns(orm);
//					if(OrderReturnStateEnum.ORFPS_Check_Fail.getCode().equals(orm.getState())
//							|| OrderReturnStateEnum.ORFPS_Complete.getCode().equals(orm.getState())
//							|| OrderReturnStateEnum.ORFPS_Cancel.getCode().equals(orm.getState())) {
//					}else {
//						ingReturnSN=orm.getSn();
//					}
                }
            }
        }
//		model.setRefundSN(ingRefundSN);
//		model.setReturnSN(ingReturnSN);
        return model;
    }

    /**
     * 订单
     */
    public static OrderModel getOrderModel(OrderInfo item, List<OrderItem> items, List<OrderItemTicketCode> ticketCodes,
                                           Map<Integer, UserSimple> userMap,
                                           Map<Integer, OrderAfterModel> lastAfterMap,
                                           List<OrderRefunds> refundList,
                                           List<OrderReturns> returnList) {
        OrderModel model = new OrderModel();
        BeanUtil.copyProperties(item, model, "items", "payItems", "shippings");

        if (!CollectionUtils.isEmpty(items)) {
            Map<String, List<OrderTicketCodeModel>> ticketCodeMap = getOrderTicketCodeModel(ticketCodes);
            items.forEach(orderItem -> {
                if (model.getSn().equals(orderItem.getOrderSn())) {
                    OrderItemModel oim = getOrderItemModel(orderItem, refundList, returnList);
                    if (!CollectionUtils.isEmpty(lastAfterMap)) {
                        oim.setLastAfter(lastAfterMap.get(oim.getId()));
                    }
                    oim.setTicketCodes(ticketCodeMap.get(oim.getProductNo()));
                    model.addItem(oim);
                }
            });
        }


        if (!CollectionUtils.isEmpty(userMap)) {
            UserSimple user = userMap.get(item.getUserId());
            if (user != null) {
                model.setUserName(user.getUserName());
                model.setUserNickName(user.getNickName());
                model.setUserRealName(user.getRealName());
                model.setUserHeadImg(user.getHeadImg());
            }
        }

        return model;
    }

    private static Map<String, List<OrderTicketCodeModel>> getOrderTicketCodeModel(List<OrderItemTicketCode> ticketCodes) {
        Map<String, List<OrderTicketCodeModel>> data = new HashMap<String, List<OrderTicketCodeModel>>();
        if (!CollectionUtils.isEmpty(ticketCodes)) {
            for (OrderItemTicketCode entity : ticketCodes) {
                List<OrderTicketCodeModel> list = data.get(entity.getProductNo());
                if (list == null) {
                    list = new ArrayList<OrderTicketCodeModel>();
                    data.put(entity.getProductNo(), list);
                }
                OrderTicketCodeModel otcm = new OrderTicketCodeModel();
                BeanUtil.copyProperties(entity, otcm);
                list.add(otcm);
            }
        }
        return data;
    }


    /**
     * 订单
     */
    public static OrderModel getOrderModel(OrderInfo item, List<OrderItem> items, List<OrderItemTicketCode> ticketCodes,
                                           Map<Integer, OrderAfterModel> lastAfterMap,
                                           Map<Integer, UserSimple> userMap, Map<String, CompanySimple> companyMap,
                                           Map<String, Artist> artistMap) {
        OrderModel model = new OrderModel();
        BeanUtil.copyProperties(item, model, "items", "payItems", "shippings");
        if (!CollectionUtils.isEmpty(items)) {
            Map<String, List<OrderTicketCodeModel>> ticketCodeMap = getOrderTicketCodeModel(ticketCodes);
            items.forEach(orderItem -> {
                if (model.getSn().equals(orderItem.getOrderSn())) {
                    OrderItemModel oim = getOrderItemModel(orderItem);
                    if (!CollectionUtils.isEmpty(lastAfterMap)) {
                        oim.setLastAfter(lastAfterMap.get(oim.getId()));
                    }
                    oim.setTicketCodes(ticketCodeMap.get(oim.getProductNo()));
                    if (!CollectionUtils.isEmpty(artistMap)) {
                        Artist artist = artistMap.get(oim.getProductNo());
                        if (artist != null) {
                            oim.setArtistNo(artist.getArtistNo());
                            oim.setArtistName(artist.getName());
                            oim.setArtistAvatar(artist.getHeadImg());
                        }
                    }
                    model.addItem(oim);
                }
            });

        }

        if (!CollectionUtils.isEmpty(userMap)) {
            UserSimple user = userMap.get(item.getUserId());
            if (user != null) {
                model.setUserName(user.getUserName());
                model.setUserNickName(user.getNickName());
                model.setUserRealName(user.getRealName());
                model.setUserHeadImg(user.getHeadImg());
            }
        }

        return model;
    }


    /**
     * 订单操作日志
     */
    public static OrderLogModel getOrderLog(OrderLog record, Map<Integer, UserSimple> userMap) {
        OrderLogModel item = new OrderLogModel();
        item.setId(record.getId());
        item.setState(record.getState());
        item.setMemo(record.getMemo());
        item.setHandleUserId(record.getCman());
        item.setHandleDate(record.getCdt());
        if (!CollectionUtils.isEmpty(userMap)) {
            UserSimple user = userMap.get(record.getCman());
            if (user != null) {
                item.setHandleUserName(user.getUserName());
                item.setHandleUserNickName(user.getNickName());
                item.setHandleUserRealName(user.getRealName());
                item.setHandleUserHeadImg(user.getHeadImg());
            }
        }
        return item;
    }

    /**
     * 订单支付
     */
    public static OrderPayModel getOrderPay(OrderPayment entity) {
        OrderPayModel model = new OrderPayModel();
        BeanUtil.copyProperties(entity, model);
        return model;
    }

    /**
     * 订单发货
     */
    public static OrderShippingModel getOrderShipping(OrderShipping entity) {
        OrderShippingModel model = new OrderShippingModel();
        BeanUtil.copyProperties(entity, model);
        return model;
    }

    /**
     * 退款
     */
    public static OrderRefundsModel getOrderRefunds(OrderRefunds entity) {
        OrderRefundsModel model = new OrderRefundsModel();
        BeanUtil.copyProperties(entity, model, "certificate", "certificateUrls");
        List<String> certificates = StrSplitUtils.parse2list(entity.getCertificate(), String.class);
        model.setCertificate(certificates);
        return model;
    }

    /**
     * 退款
     */
    public static OrderRefundsModel getOrderRefunds(OrderRefunds entity,
                                                    Map<Integer, UserSimple> userMap) {
        OrderRefundsModel model = getOrderRefunds(entity);
        if (!CollectionUtils.isEmpty(userMap)) {
            UserSimple user = userMap.get(entity.getUserId());
            if (user != null) {
                model.setUserName(user.getUserName());
                model.setUserNickName(user.getNickName());
                model.setUserRealName(user.getRealName());
                model.setUserHeadImg(user.getHeadImg());
            }
        }
        return model;
    }

    /**
     * 退款
     */
    public static OrderRefundsModel getOrderRefunds(OrderRefunds entity, List<OrderLog> records,
                                                    Map<Integer, UserSimple> userMap) {
        OrderRefundsModel model = ShopOrderConvertUtils.getOrderRefunds(entity, userMap);
        if (!CollectionUtils.isEmpty(records)) {
            records.forEach(record -> {
                model.addRecord(getOrderLog(record, userMap));
            });
        }
        return model;
    }


    /**
     * 退款退货
     */
    public static OrderReturnsModel getOrderReturns(OrderReturns entity) {
        OrderReturnsModel model = new OrderReturnsModel();
        BeanUtil.copyProperties(entity, model, "certificate", "certificateUrl");
        model.setCertificate(StrSplitUtils.parse2list(entity.getCertificate(), String.class));
        return model;
    }

    /**
     * 退款退货
     */
    public static OrderReturnsModel getOrderReturns(OrderReturns entity,
                                                    Map<Integer, UserSimple> userMap) {
        OrderReturnsModel model = getOrderReturns(entity);
        if (!CollectionUtils.isEmpty(userMap)) {
            UserSimple user = userMap.get(entity.getUserId());
            if (user != null) {
                model.setUserName(user.getUserName());
                model.setUserNickName(user.getNickName());
                model.setUserRealName(user.getRealName());
                model.setUserHeadImg(user.getHeadImg());
            }
        }
        return model;
    }

    /**
     * 退款退货
     */
    public static OrderReturnsModel getOrderReturns(OrderReturns entity, List<OrderLog> records,
                                                    Map<Integer, UserSimple> userMap) {
        OrderReturnsModel model = ShopOrderConvertUtils.getOrderReturns(entity, userMap);
        if (!CollectionUtils.isEmpty(records)) {
            records.forEach(record -> {
                model.addRecord(getOrderLog(record, userMap));
            });
        }
        return model;
    }


}
